package com.weiyuwqj.controller.account;

import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Constants;
import com.weiyuwqj.api.model.TicketParamEntity;
//import com.weiyuwqj.api.model.po.QuerySysLimitPO;
import com.weiyuwqj.api.model.vo.BasUserInviteVo;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.BasUser;
import com.weiyuwqj.api.model.BasUserSecurity;
import com.weiyuwqj.api.model.BusAccountBank;
import com.weiyuwqj.api.service.*;
//import com.weiyuwqj.api.service.haikou.QueryNoticeService;
import com.weiyuwqj.api.util.IpUtils;
import com.weiyuwqj.api.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by zhangyong on 2016/11/24.
 * 银行卡
 */
@Controller
public class AccountBankController {

    @Autowired
    BusAccountBankService accountBankService;
    @Autowired
    BasUserSecurityService userSecurityService;
    @Autowired
    BasUserService userService;
    @Autowired
    BasUserInviteService inviteService;
    @Autowired
    SysConfigTicketRuleService configTicketRuleService;


    /**
     * 显示银行卡
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/account/bank/getBankByUserId", method= RequestMethod.POST)
    public Object bankListByUserId(HttpServletRequest request) {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        Map map = new HashMap();
        map.put("userId", userId);
        map.put("status", 1);
        BusAccountBank bank = accountBankService.getOneByUserIdAndStatus(map);
        Map param = new HashMap();
        if (null != bank) {
            bank.setCardNo(StringUtils.getString(bank.getCardNo()));
            param.put("result", "success");
            param.put("bank", bank);
            return param;
        } else {
            return Message.FAIL;
        }

    }

    /**
     * 银行卡页面
     * @param request
     * @return
     */
    @RequestMapping(value="/account/bank")
    public Object bank(HttpServletRequest request){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        Map map = new HashMap();
        map.put("userId", userId);
        map.put("status", 1);
        BusAccountBank bank = accountBankService.getOneByUserIdAndStatus(map);

        if(bank!=null){
            bank.setCardNo(StringUtils.getString(bank.getCardNo()));
        }
        BasUserSecurity security=userSecurityService.getOneByUserId(userId);
        request.getSession().setAttribute("bank",bank);
        request.getSession().setAttribute("security",security);
        return "main/user/bankCard";
    }

    /**
     * 添加银行卡
     * @param request
     * @return
     */
//    @RequestMapping(value="/account/addBank")
//    public Object add(HttpServletRequest request){
//        Integer userId = (Integer) request.getSession().getAttribute("userId");
//        BusPayMemberCard busPayMemberCard = accountBankService.addBank(userId,2);
//        String memberCardUrl = accountBankService.getMemberCardUrl();
//        request.setAttribute("busPayMemberCard",busPayMemberCard);
//        request.setAttribute("memberCardUrl",memberCardUrl);
//        return "main/card";
//    }

    /**
     * 跳转银行卡页面
     * @return
     */
    @RequestMapping(value="/account/boundBankCard")
    public Object boundBankCard(){
        return "main/user/boundBankCard";
    }


    //添加银行卡
    @ResponseBody
    @RequestMapping(value = "/account/addXfBank")
    public Object addXfBank(HttpServletRequest request){

        Integer userId=(Integer)request.getSession().getAttribute("userId");

        Map cardMap= null;//queryNoticeService.userBankQuery(String.valueOf(userId));
//        if(obj==null){
//            throw new CrowdException("user.security.no_already_exists");
//        }
        if(cardMap.get("result").equals("fail")){
            return "fail";
        }
        List list=(ArrayList)cardMap.get("bankCards");
        Map param=(Map)list.get(0);
        //判断用户是否已经实名
        BasUserSecurity security=userSecurityService.getOneByUserId(userId);
        if(security.getRealnameStatus()==0){
            throw new CrowdException("user.security.no_already_exists");
        }
        //判断卡号是否存在
//        if(null !=accountBankService.getOneByCardNo(param.get("cardNo").toString())){
//            throw new CrowdException("user.bank.cardNo.already_exists");
//        }
        Map map=new HashMap();
        map.put("userId",userId);
        map.put("status",1);
        BusAccountBank bank=accountBankService.getOneByUserIdAndStatus(map);
        if(null!=bank){
            throw new CrowdException("user.bank.cardNo.already_certified");
        }

        BasUser user=userService.selectByKey(userId);
        bank=new BusAccountBank();
        bank.setAddip(IpUtils.get());
        bank.setAddtime(new Date());
        bank.setBankCode(param.get("bankCode").toString());
        bank.setBankName(param.get("bankName").toString());
        bank.setMobile(user.getMobile());
        bank.setStatus(1);
        bank.setUserId(userId);
        bank.setCardNo(param.get("cardNo").toString());
        bank.setType(Integer.parseInt(param.get("cardType").toString()));
        accountBankService.save(bank);
        //实名邀请人奖励
        BasUserInviteVo userInvite = inviteService.getOneByInviteId(userId);
        if(userInvite !=null){
            TicketParamEntity realNameParam = new TicketParamEntity();
            realNameParam.setNid(Constants.realname_invite_nid);
            realNameParam.setUserId(userInvite.getUserId());
            //realNameParam.setInviteId(userId);
            configTicketRuleService.addTicket(realNameParam);
        }
        return Message.OK;
    }

//    //查询信息
//    @ResponseBody
//    @RequestMapping(value = "/infoSearch")
//    public Object infoSearch(Integer userId){
//        Object obj = searchService.InfoSearch(userId+"");
////        if(obj==null){
////            return ;
////        }
//
//        return obj;
//    }



//    //查询银行卡信息
//    @ResponseBody
//    @RequestMapping(value = "/CardSearch")
//    public Object CardSearch(Integer userId){
//        Object obj=searchService.CardSearch(userId + "");
////        if(obj==null){
////            return ;
////        }
//
//        return obj;
//    }

/*    *//**
     * 银行卡校验
     * @param bankNo
     * @return
     *//*
    @ResponseBody
    @RequestMapping(value = "/account/bank/bankNoJudge")
    public Object bankNoJudge(String bankNo){
        return accountBankService.bankNoJudge(bankNo);
    }



    @ResponseBody
    @RequestMapping(value = "/account/limitAmount/getList",method = RequestMethod.POST )
    public Object getList(Paginator p,QuerySysLimitPO po){
        return sysLimitAmountService.getList(p,po);
    }*/

}
